package com.bts.rg.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/gkgk") //请求接口一级
public class RGController {

    @PostMapping("/cal") //请求接口二级
    public String cal(@RequestBody String req) {
        System.out.println("收到前端请求的参数为" + JSON.toJSONString(req));
        //1.req接收前端传递过来的参数
        JSONArray neuSet = JSONObject.parseArray(req);
        int[] array = new int[neuSet.size()];
        // 遍历集合，将元素赋值到数组中
        for (int i = 0; i < neuSet.size(); i++) {
            array[i] = (int) neuSet.get(i);
        }
        //2.对req解析，根据给定的题目得分计算五种情绪得分，并统计正情绪和负情绪的数量和总得分
        Map<String, Object> map = handleRequest(array);
        //3.返回结果
        return JSONObject.toJSONString(map);
    }

    public Map<String, Object> handleRequest(int[] neuSet) {
        Map<String, Object> resultMap = new HashMap<>();
        String most_per = null;
        int max_five = 0;
        int max_i = 0;
        int tot = 0;
        int ave = 0;

        //正情绪题号
        Set<Integer> posSet = new HashSet<>(Arrays.asList(0, 1, 2, 3, 6, 7, 9, 12, 16, 18, 19, 21, 24, 26, 27, 31, 32, 33, 34, 36, 37, 39, 42, 43, 45, 49, 51, 52, 57, 59, 61, 63, 65, 67, 70, 72, 73, 74, 77, 79, 80, 83, 85, 86, 89, 91, 92, 93, 95, 97, 98, 102, 103, 104));
        //负情绪题号
        Set<Integer> negSet = new HashSet<>(Arrays.asList(5, 8, 10, 11, 14, 15, 20, 23, 25, 28, 29, 30, 35, 38, 40, 41, 44, 50, 54, 58, 60, 62, 64, 66, 68, 69, 71, 75, 76, 78, 81, 82, 84, 87, 88, 90, 94, 96, 99, 100, 101));

        int neu = 0, ext = 0, ope = 0, agr = 0, con = 0;
        int pos_num = 0, pos_qul = 0, neg_num = 0, neg_qul = 0;

        for (int num = 0; num < neuSet.length; num++) {
            int answer = neuSet[num];
            if (num == 0 || num == 5 || num == 10 || num == 15 || num == 20 || num == 25 || num == 30 || num == 35 || num == 40 || num == 45 || num == 50 || num == 55) {
                if (num == 0 || num == 5 || num == 20 || num == 30) {
                    neu += 6 - answer;
                } else {
                    neu += answer;
                }
            } else if (num == 1 || num == 6 || num == 11 || num == 17 || num == 21 || num == 26 || num == 31 || num == 36 || num == 41 || num == 51 || num == 56) {
                if (num == 17 || num == 21 || num == 46) {
                    ext += 6 - answer;
                } else {
                    ext += answer;
                }
            } else if (num == 2 || num == 7 || num == 12 || num == 16 || num == 22 || num == 27 || num == 32 || num == 37 || num == 42 || num == 47 || num == 52 || num == 57) {
                if (num == 2 || num == 12 || num == 27 || num == 42 || num == 52 || num == 57) {
                    ope += 6 - answer;
                } else {
                    ope += answer;
                }
            } else if (num == 3 || num == 8 || num == 13 || num == 18 || num == 23 || num == 28 || num == 33 || num == 38 || num == 43 || num == 48 || num == 53 || num == 58) {
                if (num != 58) {
                    agr += 6 - answer;
                } else {
                    agr += answer;
                }
            } else if (num == 4 || num == 9 || num == 14 || num == 19 || num == 24 || num == 29 || num == 34 || num == 39 || num == 44 || num == 49 || num == 54 || num == 59) {
                if (num == 19 || num == 29 || num == 59) {
                    con += 6 - answer;
                } else {
                    con += answer;
                }
            }
            // 情绪 （正、负）
            // 正
            if (posSet.contains(num)) {
                pos_num++;
                pos_qul += answer;
            }
            // 负
            else if (negSet.contains(num)) {
                neg_num++;
                neg_qul += answer;
            }
        }

        int[] fivecom = {neu, ext, ope, agr, con};

        //求总分和最大分的情绪名称
        if (fivecom[0] != fivecom[1] && fivecom[0] != fivecom[2] && fivecom[0] != fivecom[3] && fivecom[0] != fivecom[4] && fivecom[1] != fivecom[2] && fivecom[1] != fivecom[3] && fivecom[1] != fivecom[4] && fivecom[2] != fivecom[3] && fivecom[2] != fivecom[4] && fivecom[3] != fivecom[4]) {
            for (int j = 0; j < 5; j++) {
                tot += fivecom[j];
                if (max_five < fivecom[j]) {
                    max_five = fivecom[j];
                    max_i = j;
                }
            }
        }
        //情绪总分除以5算出平均分
        ave = tot / 5;

        //根据最大情绪名称得出所属的人格类型
        switch (max_i) {
            case 0:
                most_per = "敏感型";
                break;
            case 1:
                most_per = "外向型";
                break;
            case 2:
                most_per = "开放型";
                break;
            case 3:
                most_per = "宜人型";
                break;
            case 4:
                most_per = "尽责型";
                break;
            default:
                // 如果没有匹配到任何一个 case，这里是一个可选的默认分支
                break;
        }


        int fuwu = neuSet[105];
        int cehua = neuSet[106];
        int fenxi = neuSet[107];
        int guanli = neuSet[108];
        int yishu = neuSet[109];
        int keyan = neuSet[110];
        int wenyu = neuSet[111];
        int renwen = neuSet[112];

        List<String> resultList = new ArrayList<>();
        if (ext >= ave && agr >= ave && fuwu >= 3) // range(46,39)
        {
            resultList.add("\n服务类:（宠物服务、餐饮、运动健身、销售行政/商务、销售管理、销售技术支持、" + "教育培训、旅游服务、汽车销售与服务、运营客服、物流、采购、进出口贸易）\n");
        }
        if (neu >= ave && ope >= ave && yishu >= 3) // range(39,42)
        {
            resultList.add("\n艺术类:（视觉设计、用户研究、高端设计职位、其他设计职位、非视觉设计、交互设计、" + "房地产规划开发、设计装修与市政建设）\n");
        }
        if (agr >= ave && con >= ave && guanli >= 3) // range(39, 49)
        {
            resultList.add("\n管理类:（高级管理、高端产品职位、产品经理、物业管理、高端房地产职位、高端供应链职位、项目管理、教育行政）\n");
        }
        if (ext >= ave && con >= ave && wenyu >= 3) {
            resultList.add("\n文娱类:（影视媒体、采编/写作/出版、" + "教师、IT培训、职业培训、特长培训、" + "医学营销/媒体、" + "旅游产品开发/策划)\n");
        }
        if (ope >= ave && con >= ave && keyan >= 3) {
            resultList.add("\n科研类:（质量安全、新能源、汽车制造、机械设计/制造、化工、服装/纺织/皮革、" + "后端开发、移动开发、硬件开发、高端技术职位、运维/技术支持、数据、电子/半导体、前端开发、人工智能、通信、其他技术职位、测试、" + "临床试验、医生/医技、健康整形、生物制药、医疗器械、" + "教育产品研发）\n");
        }
        if (con >= ave && renwen >= 3) //49
        {
            resultList.add("\n人文类:（咨询/调研、律师、翻译、" + "财务、人力资源、行政、法务）\n");
        }
        if (ope >= ave && agr >= ave && con >= ave && cehua >= 3) {
            resultList.add("\n策划类:（生产营运、" + "高端运营职位、运营、其他运营职位、编辑）\n");
        }
        if (con >= ave && fenxi >= 3) //49
        {
            resultList.add("\n分析类:（互联网金融、保险、银行、投融资、风控、税务审计、证券、" + "高端市场职位、市场/营销、政府事务、" + "房地产：房地产经纪）\n");
        }

        Map<String, Integer> scores = new HashMap<>();
        scores.put("neu", neu);
        scores.put("ext", ext);
        scores.put("ope", ope);
        scores.put("agr", agr);
        scores.put("con", con);
        System.out.println("各种情绪得分为:" + JSONObject.toJSON(scores));
        System.out.println("总分为:" + tot);
        System.out.println("正情绪得分:" + pos_qul + " 负情绪得分为:" + neg_qul);
        System.out.println("您最突出的性格特点为:" + most_per);
        System.out.println("综合您的性格与能力分布特点，您较适合的职业为:");
        resultList.forEach(System.out::println);
        resultMap.put("most_per", most_per);
        resultMap.put("work", resultList);
        resultMap.put("scores", scores);
        resultMap.put("tot", tot);
        resultMap.put("pos_qul", pos_qul);
        resultMap.put("neg_qul", neg_qul);
        return resultMap;
    }
}
